package com.mall.project.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mall.common.core.model.response.ApiResponses;
import com.mall.common.core.model.supers.SuperController;
import com.mall.common.log.annotation.Log;
import com.mall.common.log.enums.BusinessType;
import com.mall.common.security.annotation.PreAuth;
import com.mall.project.sys.model.entity.OperateLog;
import com.mall.project.sys.model.parm.OperateLogPARM;
import com.mall.project.sys.service.IOperateLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 操作日志记录 前端控制器
 * </p>
 *
 * @author Sam
 * @since 2021-04-07
 */
@Api(tags = {"操作日志记录"})
@RestController
@RequestMapping("/operateLog")
public class OperateLogRestController extends SuperController {

    @Resource
    private IOperateLogService operateLogService;

    @PreAuth
    @Log(title = "分页查询操作日志", businessType = BusinessType.SELECT)
    @ApiOperation("分页查询操作日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cursor", value = "当前页数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页条数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "title", value = "模块标题", paramType = "query"),
            @ApiImplicitParam(name = "businessType", value = "业务类型（0其它 1新增 2修改 3删除）", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "requestMethod", value = "请求方式(get/post/put/delete)", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "操作状态（0正常 1异常）", dataType = "int", paramType = "query"),
    })
    @GetMapping
    public ApiResponses<IPage<OperateLog>> page(@ApiIgnore @ModelAttribute OperateLogPARM operateLogPARM) {
        LambdaQueryWrapper<OperateLog> wrappers = Wrappers.<OperateLog>lambdaQuery()
                .select(OperateLog::getOperId, OperateLog::getTitle, OperateLog::getBusinessType, OperateLog::getMethod,
                        OperateLog::getRequestMethod, OperateLog::getOperType, OperateLog::getOperUrl, OperateLog::getOperIp,
                        OperateLog::getRunTime, OperateLog::getStatus, OperateLog::getCreateTime)
                .like(StringUtils.isNotBlank(operateLogPARM.getTitle()), OperateLog::getTitle, operateLogPARM.getTitle())
                .eq(Objects.nonNull(operateLogPARM.getBusinessType()), OperateLog::getBusinessType, operateLogPARM.getBusinessType())
                .eq(StringUtils.isNotBlank(operateLogPARM.getRequestMethod()), OperateLog::getRequestMethod, operateLogPARM.getRequestMethod())
                .gt(Objects.nonNull(operateLogPARM.getStartTime()), OperateLog::getCreateTime, operateLogPARM.getStartTime())
                .lt(Objects.nonNull(operateLogPARM.getEndTime()), OperateLog::getCreateTime, operateLogPARM.getEndTime())
                .eq(Objects.nonNull(operateLogPARM.getStatus()), OperateLog::getStatus, operateLogPARM.getStatus());
        IPage<OperateLog> logIPage = operateLogService.page(this.getPage(), wrappers);
        return success(logIPage);
    }

    @PreAuth
    @Log(title = "单条查询操作日志", businessType = BusinessType.SELECT)
    @ApiOperation("单条查询操作日志")
    @GetMapping(value = "/{id}")
    public ApiResponses<OperateLog> getById(@PathVariable("id") Long id) {
        return success(operateLogService.getById(id));
    }

    @PreAuth
    @Log(title = "删除操作日志", businessType = BusinessType.DELETE)
    @ApiOperation("删除操作日志")
    @DeleteMapping("/{idList}")
    public ApiResponses remove(@PathVariable List<Long> idList) {
        operateLogService.removeByIds(idList);
        return success();
    }

    @PreAuth
    @Log(title = "清空操作日志", businessType = BusinessType.CLEAN)
    @ApiOperation("清空操作日志")
    @DeleteMapping("/clean")
    public ApiResponses clean() {
        operateLogService.cleanLog();
        return success();
    }

}

